package com.bigger.aop;

import com.bigger.constant.ErrorConstant;
import com.bigger.context.BaseContext;
import com.bigger.entity.Employee;
import com.bigger.exception.PowerException;
import com.bigger.mapper.EmployeeMapper;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PowerAspect {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 此为aop对对应的增删改查做对应的权限校验
     * 请注意方法命名规范
     */
    @Pointcut("execution(* com.bigger.controller.*.update(..)) ||" +
            "execution(* com.bigger.controller.*.add*(..)) ||" +
            "execution(* com.bigger.controller.*.delete(..)) ||"+
            "execution(* com.bigger.controller.CommonController.upload(..))")
    private void power(){}

    //权限校验
    @Before("power()")
    public void powerCheck(){
        Employee employee = employeeMapper.selectById(BaseContext.getCurrentId());
        if (employee.getJob() != 1){
            throw new PowerException(ErrorConstant.INSUFFICIENT_PRIVILEGES);
        }
    }
}
